package com.wpen.unit.infra.repository.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wpen.unit.utils.StringUtil;
import com.wpen.unit.exception.UpdateZeroException;

import com.wpen.unit.infra.mybatis.system.mapper.DictionaryMapper;
import com.wpen.unit.infra.mybatis.system.po.DictionaryPo.DictionaryColumns;
import com.wpen.unit.infra.mybatis.system.po.DictionaryPo;
import com.wpen.unit.infra.repository.system.DictionaryRepository;

/**
 * 系统数据字典维护.
 * 
 * @author Wang Peng
 * @date 2022-12-23
 *
 */
@Repository
@Transactional
public class DictionaryRepositoryImpl extends ServiceImpl<DictionaryMapper, DictionaryPo> implements DictionaryRepository{
    
    @Override
    public long count(DictionaryPo dictionaryPo){
		QueryWrapper<DictionaryPo> queryWrapper = createQuery(dictionaryPo);
		return super.count(queryWrapper);
    }

	@Override
    public List<DictionaryPo> queryList(int index, int size, DictionaryPo dictionaryPo){
        QueryWrapper<DictionaryPo> queryWrapper = createQuery(dictionaryPo);
		if (size > 0) {
			Page<DictionaryPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		//queryWrapper.orderBy(true, false, DictionaryColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
    }
    
    private QueryWrapper<DictionaryPo> createQuery(DictionaryPo dictionaryPo) {
        QueryWrapper<DictionaryPo> queryWrapper = Wrappers.query();
        if (dictionaryPo.getId() != null) {
            queryWrapper.eq(DictionaryColumns.ID.name(), dictionaryPo.getId());
        }
        if (StringUtil.isNotEmpty(dictionaryPo.getDictGroup())) {
            queryWrapper.eq(DictionaryColumns.DICT_GROUP.name(), dictionaryPo.getDictGroup());
        }
        if (StringUtil.isNotEmpty(dictionaryPo.getDictLabel())) {
            queryWrapper.eq(DictionaryColumns.DICT_LABEL.name(), dictionaryPo.getDictLabel());
        }
        if (StringUtil.isNotEmpty(dictionaryPo.getDictValue())) {
            queryWrapper.eq(DictionaryColumns.DICT_VALUE.name(), dictionaryPo.getDictValue());
        }
        //if (StringUtil.isNotEmpty(dictionaryPo.getDictStyle())) {
        //    queryWrapper.eq(DictionaryColumns.DICT_STYLE.name(), dictionaryPo.getDictStyle());
        //}
        if (StringUtil.isNotEmpty(dictionaryPo.getDictDefault())) {
            queryWrapper.eq(DictionaryColumns.DICT_DEFAULT.name(), dictionaryPo.getDictDefault());
        }
        //if (dictionaryPo.getOrders() != null) {
        //    queryWrapper.eq(DictionaryColumns.ORDERS.name(), dictionaryPo.getOrders());
        //}
        //if (StringUtil.isNotEmpty(dictionaryPo.getDictPid())) {
        //    queryWrapper.eq(DictionaryColumns.DICT_PID.name(), dictionaryPo.getDictPid());
        //}
        //if (StringUtil.isNotEmpty(dictionaryPo.getDictDesc())) {
        //    queryWrapper.eq(DictionaryColumns.DICT_DESC.name(), dictionaryPo.getDictDesc());
        //}
        if (StringUtil.isNotEmpty(dictionaryPo.getStatus())) {
            queryWrapper.eq(DictionaryColumns.STATUS.name(), dictionaryPo.getStatus());
        }
        //if (dictionaryPo.getVersion() != null) {
        //    queryWrapper.eq(DictionaryColumns.VERSION.name(), dictionaryPo.getVersion());
        //}
        return queryWrapper;
    }
    
    @Override
    public DictionaryPo queryFirst(DictionaryPo dictionaryPo) {
        QueryWrapper<DictionaryPo> queryWrapper = Wrappers.query();
        if (dictionaryPo.getId() != null) {
            queryWrapper.eq(DictionaryColumns.ID.name(), dictionaryPo.getId());
        }
        if (StringUtil.isNotEmpty(dictionaryPo.getDictGroup())) {
            queryWrapper.eq(DictionaryColumns.DICT_GROUP.name(), dictionaryPo.getDictGroup());
        }
        if (StringUtil.isNotEmpty(dictionaryPo.getDictLabel())) {
            queryWrapper.eq(DictionaryColumns.DICT_LABEL.name(), dictionaryPo.getDictLabel());
        }
        if (StringUtil.isNotEmpty(dictionaryPo.getDictValue())) {
            queryWrapper.eq(DictionaryColumns.DICT_VALUE.name(), dictionaryPo.getDictValue());
        }
        //if (StringUtil.isNotEmpty(dictionaryPo.getDictStyle())) {
        //    queryWrapper.eq(DictionaryColumns.DICT_STYLE.name(), dictionaryPo.getDictStyle());
        //}
        if (StringUtil.isNotEmpty(dictionaryPo.getDictDefault())) {
            queryWrapper.eq(DictionaryColumns.DICT_DEFAULT.name(), dictionaryPo.getDictDefault());
        }
        //if (dictionaryPo.getOrders() != null) {
        //    queryWrapper.eq(DictionaryColumns.ORDERS.name(), dictionaryPo.getOrders());
        //}
        //if (StringUtil.isNotEmpty(dictionaryPo.getDictPid())) {
        //    queryWrapper.eq(DictionaryColumns.DICT_PID.name(), dictionaryPo.getDictPid());
        //}
        //if (StringUtil.isNotEmpty(dictionaryPo.getDictDesc())) {
        //    queryWrapper.eq(DictionaryColumns.DICT_DESC.name(), dictionaryPo.getDictDesc());
        //}
        if (StringUtil.isNotEmpty(dictionaryPo.getStatus())) {
            queryWrapper.eq(DictionaryColumns.STATUS.name(), dictionaryPo.getStatus());
        }
        //if (dictionaryPo.getVersion() != null) {
        //    queryWrapper.eq(DictionaryColumns.VERSION.name(), dictionaryPo.getVersion());
        //}
		//queryWrapper.orderBy(true, false, DictionaryPoColumns.ID.name());
		return super.getOne(queryWrapper);
    }
    
    @Override
    public DictionaryPo queryById(Serializable id) {
        return super.getById(id);
    }
    
    @Override
    public boolean insert(DictionaryPo dictionaryPo) {
        dictionaryPo.setVersion(1);
        dictionaryPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return super.save(dictionaryPo);
    }

    @Override
    public boolean insertBatch(List<DictionaryPo> listDictionaryPos) {
        int rs = getBaseMapper().insertBatchSomeColumn(listDictionaryPos);
        return SqlHelper.retBool(rs);
    }

    @Override
    public boolean updateById(DictionaryPo dictionaryPo) {
        UpdateWrapper<DictionaryPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(DictionaryColumns.ID.name(), dictionaryPo.getId());
        updateWrapper.eq(DictionaryColumns.VERSION.name(), dictionaryPo.getVersion());
        dictionaryPo.setVersion(dictionaryPo.getVersion() + 1);
        boolean rs = super.update(dictionaryPo, updateWrapper);
        // 根据ID更新所有字段，空值也更新
        // int num = getBaseMapper().alwaysUpdateSomeColumnById(ucpBatchInfoPo);
        // boolean rs = SqlHelper.retBool(num);
        if (!rs) {
            throw new UpdateZeroException();
        }
        return rs;
    }

    @Override
    public boolean deleteByIds(Serializable[] ids) {
        UpdateWrapper<DictionaryPo> updateWrapper = Wrappers.update();
		updateWrapper.in(DictionaryColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
    }
    
//    public DictionaryPo queryByExample(DictionaryPo dictionaryPo) {
//        QueryWrapper<DictionaryPo> queryWrapper = Wrappers.query();
//        if (dictionaryPo.getId() != null) {
//            queryWrapper.eq(DictionaryColumns.ID.name(), dictionaryPo.getId());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getDictGroup())) {
//            queryWrapper.eq(DictionaryColumns.DICT_GROUP.name(), dictionaryPo.getDictGroup());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getDictLabel())) {
//            queryWrapper.eq(DictionaryColumns.DICT_LABEL.name(), dictionaryPo.getDictLabel());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getDictValue())) {
//            queryWrapper.eq(DictionaryColumns.DICT_VALUE.name(), dictionaryPo.getDictValue());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getDictStyle())) {
//            queryWrapper.eq(DictionaryColumns.DICT_STYLE.name(), dictionaryPo.getDictStyle());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getDictDefault())) {
//            queryWrapper.eq(DictionaryColumns.DICT_DEFAULT.name(), dictionaryPo.getDictDefault());
//        }
//        if (dictionaryPo.getOrders() != null) {
//            queryWrapper.eq(DictionaryColumns.ORDERS.name(), dictionaryPo.getOrders());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getDictPid())) {
//            queryWrapper.eq(DictionaryColumns.DICT_PID.name(), dictionaryPo.getDictPid());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getDictDesc())) {
//            queryWrapper.eq(DictionaryColumns.DICT_DESC.name(), dictionaryPo.getDictDesc());
//        }
//        if (StringUtil.isNotEmpty(dictionaryPo.getStatus())) {
//            queryWrapper.eq(DictionaryColumns.STATUS.name(), dictionaryPo.getStatus());
//        }
//        if (dictionaryPo.getVersion() != null) {
//            queryWrapper.eq(DictionaryColumns.VERSION.name(), dictionaryPo.getVersion());
//        }
//		Page<DictionaryPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, DictionaryPoColumns.ID.name());
//    }

}
